#include "Channels.h"
#include "ChannelManager.h"
#include "Resource.h"
#include "String.h"
#include "SharedPtr.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "WorldPacket.h"
#include "Towns.h"
#include "Effects.h"
#include "EffectManager.h"
#include "EffectModels.h"
#include "EffectModelManager.h"
#include "Users.h"
#include "UserManager.h"
#include "XMLData.h"
#include "Towns.h"
#include "Message.h"
#include "XMLData.h"
#include "OpcodesEx.h"
#include "VipCardManager.h"
#include "Tools.h"
#include "WSSocket.h"


#include "twmjn/TWMJOpcode.h"
#include "twmjn/TWMJAIInterface.h"
#include "twmjn/TWMJGameStruct.h"
#include "twmjn/TWMJXmlDataManager.h"
#include "twmjn/TWMJHallManager.h"
#include "GameDefine.h"
#include LOGIC_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include XMLDATA_MGR_PATH
#include LOGIC_MGR_PATH
//-------------------------------------------
void HallManager::AfterLoginWorldOk(WSSocketPtr &socket, UserPtr &pUser, CharPtr &pChr)
{
	WorldPacket packet;
	// WorldPacket packet;
	
	// 判断是否直接进牌桌接着打牌
//	using AIScript::TWMJAICreatureInterface;
	TWMJAICreatureInterface *pChrAIInfe = TO_TWMJCREATURE_INTERFACE(pChr->getAIInterface());
	if (NULL == pChrAIInfe)
		return;
	// 房间
	TownPtr pTown = pChrAIInfe->GetTown();
	ChannelPtr pchan = pChrAIInfe->GetChannelPtr();
	if (!pchan.isNull())
	{
	
		// 进入房间
		AIMaJiangChannelBaseInterface *pAiChannelInfe= TO_MJCHANNEL_INTERFACE(pchan->getAIInterface());
		if (/*pAiChannelInfe->GetUnit()->getUInt32Field("ai") == 150*/true)
		{
			WorldPacket packet;
			std::list<TownPtr> town_list;
			uint8 room_num = sTownMgr.getDBCount("flag = 3");
			sTownMgr.getTownsList(&town_list, 0, room_num, "flag = 3");
			if(sProtocolsMgr.CreateCharInforPacket(&packet,pChr))
				socket->SendPacket(&packet);
			// if (sProtocolsMgr.CreateRoomListPacket(&packet, &town_list))
			// 	socket->SendPacket(&packet);
			if (sProtocolsMgr.CreateEnterTownPacket(&packet, 200,1))
				socket->SendPacket(&packet);
			if (!pAiChannelInfe->JoinChannel(pChr))
			{
				pChrAIInfe->SetChannelNull();
				pChrAIInfe->SetTownNull();
				if (sProtocolsMgr.CreateEnterTownPacket(&packet, 0, 1))
				socket->SendPacket(&packet);
			}
		}
		else 
		{
			if (sProtocolsMgr.CreateCharInforPacket(&packet, pChr))
				sGLMgr.SendProtocolsToChr(pChr, &packet);
			std::list<TownPtr> town_list;
			uint8 room_num = sTownMgr.getDBCount("flag = 3");
			sTownMgr.getTownsList(&town_list, 0, room_num, "flag = 3");
		

			if (sProtocolsMgr.CreateRoomListPacket(&packet, &town_list))
			{
				socket->SendPacket(&packet);
			}
			
			// 通知直接去牌桌
			if (sProtocolsMgr.CreateEnterTownPacket(&packet, pTown->getHandle(), 1))
				socket->SendPacket(&packet);
			if (!pAiChannelInfe->JoinChannel(pChr))
			{
				// 如果失败，进入大厅
				if (sProtocolsMgr.CreateEnterTownPacket(&packet, 0, 1))
					socket->SendPacket(&packet);
				pChrAIInfe->SetChannelNull();
				pChrAIInfe->SetTownNull();
			}
		
		}
	}
	else
	{
		if (sProtocolsMgr.CreateCharInforPacket(&packet, pChr))
			sGLMgr.SendProtocolsToChr(pChr, &packet);
		std::list<TownPtr> town_list;
		uint8 room_num = sTownMgr.getDBCount("flag = 3");
		sTownMgr.getTownsList(&town_list, 0, room_num, "flag = 3");

		if (sProtocolsMgr.CreateRoomListPacket(&packet, &town_list))
		{
			socket->SendPacket(&packet);
		}
		
		// 进入大厅
		if (sProtocolsMgr.CreateEnterTownPacket(&packet,0 , 1))
			socket->SendPacket(&packet);
	}
	
	// 下发欢迎消息
	sGLMgr.LoginWellcomeMessage(pUser);
}